#include "setzeropointdialog.h"
#include "ui_setzeropointdialog.h"
#include "../CommunicateDelegate/communicatenormaldelegate.h"
#include <QMessageBox>
#include "globaldata.h"
#include "instructionEnum.h"

SetZeroPointDialog::SetZeroPointDialog(int styleId,CommunicateNormalDelegate *communicateNormalDelegateIn,
                                       GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SetZeroPointDialog)
{
    communicateNormalDelegate=communicateNormalDelegateIn;
    globalData=globalDataIn;

    ui->setupUi(this);
        move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif
    model_axisgroupCalibrateInfo = new QStandardItemModel();
    QStringList labels4 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_axisgroupCalibrateInfo->setHorizontalHeaderLabels(labels4);
    ui->tableView_axisgroupCalibrateInfo->setColumnWidth(1,120);

    QStringList strHead;
    strHead<< tr("J1") << tr("J2") << tr("J3") << tr("J4") << tr("J5") << tr("J6");
    ui->position_tableWidget->setHorizontalHeaderLabels(strHead);
    ui->position_tableWidget->setColumnCount(8);
    ui->position_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->position_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->tableWidget_zeroOffset->setHorizontalHeaderLabels(strHead);
    ui->tableWidget_zeroOffset->setColumnCount(2);
    ui->tableWidget_zeroOffset->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->tableWidget_zeroOffset->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->comboBox_axisId->clear();
    for(int i=0;i<D_ROBOT_AXIS_MAX;i++)
    {
        QString tmpStr;
        tmpStr="J";
        tmpStr+=QString::number(i+1);
        ui->comboBox_axisId->insertItem(i,tmpStr);
    }


}

void SetZeroPointDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->tabWidget->setGeometry(0,0,460,440);

        ui->pushButton_getCalibrateInformation->setGeometry(510,350,70,40);
        ui->returnBtn->setGeometry(510,400,70,40);

        ui->tableView_axisgroupCalibrateInfo->setGeometry(0,30,450,380);

        ui->position_tableWidget->setGeometry(15,34,290,320);
        ui->tableWidget_zeroOffset->setGeometry(330,72,110,280);
        ui->recordPoint_Btn->setGeometry(20,360,80,40);
        ui->button_clearPoint->setGeometry(150,360,80,40);
        ui->button_calculateZero->setGeometry(250,360,80,40);
        ui->button_setZeroOffset->setGeometry(360,360,80,40);
        break;
    default:
        break;
    }
}

SetZeroPointDialog::~SetZeroPointDialog()
{
    delete ui;
}

void SetZeroPointDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void SetZeroPointDialog::updateZeroCalculatePointView(std::vector<struct PointPro> pointList_zeroCalculateIn)
{
    ui->position_tableWidget->clear();
    //init 点数据
//    ui->position_tableWidget->setHorizontalHeaderLabels(strHead);//设置头部

    int rowCount = pointList_zeroCalculateIn.size();
    ui->position_tableWidget->setRowCount(rowCount);

    QStringList hStrList;
    for(int i = 0; i < rowCount; i++)
    {
        hStrList << tr("P")+QString::number(i+1);
        ui->position_tableWidget->setVerticalHeaderLabels(hStrList);
    }


    for(int i = 0; i < rowCount; i ++)
    {
        for(int j = 0; j < 6; j ++)
        {
            if(j < pointList_zeroCalculateIn[i].positionValue.size() )
            {
                QString data = QString::number(pointList_zeroCalculateIn[i].positionValue[j],'f',2);
                ui->position_tableWidget->setItem(i, j, new QTableWidgetItem(data));
            }
            else
            {
                ui->position_tableWidget->setItem(i, j, new QTableWidgetItem(""));
            }
        }
    }

}

void SetZeroPointDialog::updateZeroCalculateResultView(AxisZeroOffsetInfo zeroInfo)
{
    ui->lineEdit_maxError->setText(QString::number(zeroInfo.maxError,'f',3));
    ui->tableWidget_zeroOffset->clear();
    //init 点数据
//    ui->tableWidget_zeroOffset->setHorizontalHeaderLabels(strHead);//设置头部

    int rowCount = zeroInfo.zeroOffset.size();
    ui->tableWidget_zeroOffset->setRowCount(rowCount);

    QStringList hStrList;
    for(int i = 0; i < rowCount; i++)
    {
        hStrList << tr("J")+QString::number(i+1);
        ui->tableWidget_zeroOffset->setVerticalHeaderLabels(hStrList);
    }

    for(int i = 0; i < rowCount; i ++)
    {
        QString data = QString::number(zeroInfo.zeroOffset[i],'f',3);
        ui->tableWidget_zeroOffset->setItem(i, 0, new QTableWidgetItem(data));
    }
}

void SetZeroPointDialog::on_LoadBtn_clicked()
{

//     emit calibrateAllAxisSignal();

    if(ENUM_COMMAND_RUN==globalData->globalCurrentState_motion)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("运行状态禁止设置,请停止程序再尝试"));
        return ;
    }
    QMessageBox message(QMessageBox::Question, "提示", "是否记录当前位置为机器人所有轴的零点！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalDelegate->setRobotAllAxisCaliberate(returnFlag);
//    if(1==returnFlag)
//    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("一键校零成功"));
//    }
//    else
//    {
//        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("一键校零失败"));
//    }

}

void SetZeroPointDialog::on_returnBtn_clicked()
{
    this->hide();
}

//void SetZeroPointDialog::on_set_zero_positon(bool isCorrect)
//{

//        emit calibrateAllAxisSignal();

//}

void SetZeroPointDialog::on_radioButton_clicked(bool checked)
{
    on_radioButton_allowJog_clicked(checked);
}

void SetZeroPointDialog::on_radioButton_allowJog_clicked(bool checked)
{
    emit allowCalibrateJogSignal(checked);
}

void SetZeroPointDialog::on_pushButton_oneAxisCalibrate_clicked()
{
//    emit calibrateOneAxisSignal(ui->comboBox_axisId->currentIndex());

    if(ENUM_COMMAND_RUN==globalData->globalCurrentState_motion)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("运行状态禁止设置,请停止程序再尝试"));
        return ;
    }
    QMessageBox message(QMessageBox::Question, "提示", "是否记录当前位置为机器人选定轴的零点！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalDelegate->setRobotSingleAxisCaliberate(ui->comboBox_axisId->currentIndex(),returnFlag);
//    if(1==returnFlag)
//    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("单轴校零成功"));
//    }
//    else
//    {
//        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("单轴校零失败"));
//    }
}

void SetZeroPointDialog::on_pushButton_getCalibrateInformation_clicked()
{
    emit getCalibrateInformation();
}


void SetZeroPointDialog::showCalibrateInfo(S_AXISGROUP_ZERO_INFORMATION zeroInformationOut)
{


    model_axisgroupCalibrateInfo->clear();
    //定义item
    QStandardItem* item = 0;
    int lineSize=4;

    QString magementState;
    if(0==zeroInformationOut.state)
    {
        magementState="初始化中";
    }
    else if(1==zeroInformationOut.state)
    {
        magementState="正常";
    }
    else if(2==zeroInformationOut.state)
    {
        magementState="MD5校验错误";
    }
    else if(3==zeroInformationOut.state)
    {
        magementState="零点文件信息不全";
    }
    item = new QStandardItem("零点管理模块状态");
    model_axisgroupCalibrateInfo->setItem(0,0,item);
    item = new QStandardItem(magementState);
    model_axisgroupCalibrateInfo->setItem(0,1,item);

    for(int i=0;i<zeroInformationOut.axisZeroState.size();i++)
    {
        QString axisZeroState;
        if(0==zeroInformationOut.axisZeroState[i])
        {
            axisZeroState="未校准";
        }
        else if(1==zeroInformationOut.axisZeroState[i])
        {
            axisZeroState="已校准";
        }
        else if(2==zeroInformationOut.axisZeroState[i])
        {
            axisZeroState="掉电无零点";
        }
        else if(3==zeroInformationOut.axisZeroState[i])
        {
            axisZeroState="文件校验错误";
        }
        item = new QStandardItem(QString("%1轴零点状态").arg(i+1));
        model_axisgroupCalibrateInfo->setItem(lineSize*i+2,0,item);
        item = new QStandardItem(axisZeroState);
        model_axisgroupCalibrateInfo->setItem(lineSize*i+2,1,item);

        item = new QStandardItem(QString("%1轴绝对零点编码器值").arg(i+1));
        model_axisgroupCalibrateInfo->setItem(lineSize*i+3,0,item);
        item = new QStandardItem(QString("%1").arg(zeroInformationOut.absoluteZeroMotorValue[i]));
        model_axisgroupCalibrateInfo->setItem(lineSize*i+3,1,item);

        item = new QStandardItem(QString("%1轴机械零点偏移值").arg(i+1));
        model_axisgroupCalibrateInfo->setItem(lineSize*i+4,0,item);
        item = new QStandardItem(QString::number(zeroInformationOut.jointMechanicalZeroValue[i],'f',3));
        model_axisgroupCalibrateInfo->setItem(lineSize*i+4,1,item);


    }


    ui->tableView_axisgroupCalibrateInfo->setModel(model_axisgroupCalibrateInfo);
    ui->tableView_axisgroupCalibrateInfo->resizeColumnToContents(0);
    ui->tableView_axisgroupCalibrateInfo->resizeColumnToContents(1);
    ui->tableView_axisgroupCalibrateInfo->resizeColumnToContents(2);
    ui->tableView_axisgroupCalibrateInfo->show();

}

void SetZeroPointDialog::on_recordPoint_Btn_clicked()
{
    emit recordPoint_signal();


}

void SetZeroPointDialog::on_button_clearPoint_clicked()
{
    emit clearPoint_signal();
}

void SetZeroPointDialog::on_button_calculateZero_clicked()
{
//    emit calculateZero_signal();
    QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("功能未开放"));
}

void SetZeroPointDialog::on_button_setZeroOffset_clicked()
{
//    emit setZeroOffset_signal();
       QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("功能未开放"));
}

void SetZeroPointDialog::on_pushButton_clearRobotAllEncoderError_clicked()
{

    if(ENUM_COMMAND_RUN==globalData->globalCurrentState_motion)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("运行状态禁止设置,请停止程序再尝试"));
        return ;
    }
    QMessageBox message(QMessageBox::Question, "提示", "是否复位当前机器人所有轴的编码器掉电错误！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalDelegate->clearMotorBatteryError(returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("复位成功"));
    }
    else
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("复位失败"));
    }

}

void SetZeroPointDialog::on_pushButton_singleAxisEncoderError_clicked()
{
    if(ENUM_COMMAND_RUN==globalData->globalCurrentState_motion)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("运行状态禁止设置,请停止程序再尝试"));
        return ;
    }
    QMessageBox message(QMessageBox::Question, "提示", "是否复位单轴的编码器掉电错误！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalDelegate->clearMotorBatteryErrorByAxis(ui->comboBox_axisId->currentIndex(),returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("复位成功"));
    }
    else
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("复位失败"));
    }
}
